package com.hgx.servlet.demo.test;/**
 * Created by liujun on 2018/5/28.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author Administrator
 * @create 2018-05-28 13:57
 */
public class TaskB {

    public int readFile(String fileName) {

        long startTime = System.currentTimeMillis();
        FileInputStream fis = null;
        try {
            ByteBuffer buffer = ByteBuffer.allocate(8 * 1024 * 1024);
            fis = new FileInputStream(new File(fileName));
            byte[] datas = new byte[1024];
            while (fis.read(datas) != -1) {
                buffer.put(datas);
            }

//            System.out.println("time: "+(System.currentTimeMillis() - startTime)+" , len: "+buffer.position());
            return buffer.position();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return -1;
    }
}
